#!/usr/bin/perl
use strict;
use POSIX ":sys_wait_h";

open FV, "http_mp3.txt";
my $cnt = 50;
my $all = 0;
my @arr;
my $line;

while ($line = <FV>) {
	chomp($line);
	push @arr, $line;
	$all ++;
}

foreach my $idx (0 .. $cnt-1) {
	my $line_cnt = $idx;
	my $pid = fork();
	if ($pid>0) {
		next;
	} else {
		while ($line_cnt <= $all) {
			system("wget --user-agent=\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31\" \"$arr[$line_cnt]\" -O ./video/$idx.mp3");
=prod
			my $cmd = "wget -d  \"$arr[$line_cnt]\" 2>&1  | grep Location | awk -F\"Location: \" '{print \$2}' | grep http | grep http | grep -v following";
			my $url_302 = `$cmd`;
			 $url_302 =~ s/\n//g;
			 $url_302 =~ s/\r//g;
			 $url_302 =~ s/\t//g;
			 $url_302 =~ s/ //g;
			 #$cmd = "curl --head  \"$url_302\" 2>&1  | grep Length";
			 #my $head = `$cmd`;
			 #$head =~ s/\n//g;
			 #$head =~ s/\r//g;
			 #$head =~ s/\t//g;
			 #$head =~ s/ //g;
			 #print $head, "\n";
			 `echo $url_302 >> head/$idx.log`;
=cut
			 $line_cnt += $cnt;
		}
		exit;
	}
}

foreach my $idx (0 .. $cnt-1) {
	wait();
}

print "MAIN exit\n";
